package com.turing.post.salesmanage.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.salesmanage.util.OrganizeInfDetail;
import com.turing.post.salesmanage.util.OrganizeInfDetailDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * @author zxp
 * @Dec 22, 2012
 */
@Stateless(mappedName = "salesmanage.impl.OrganizeInfDetailDaoImpl")
@Remote
@Local
public class OrganizeInfDetailDaoImpl implements OrganizeInfDetailDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public List<OrganizeInfDetail> getOrganizeInfDetailByOrganizeId(
			BigInteger organizeId) {
		if(organizeId==null){
			return null;
		}
		String jpql = "select * from OrganizeInfDetail where organizeId=?1 order by seqNo";
		Query query = _em.createNativeQuery(jpql, OrganizeInfDetail.class);
		query.setParameter(1, organizeId);
		return query.getResultList();
	}

	@Override
	public OrganizeInfDetail getOrganizeInfDetailByOrganizeInfDetail(
			OrganizeInfDetail organizeInfDetail) {
		if (organizeInfDetail.getOrganizeId() != null
				&& organizeInfDetail.getSeqNo() != null) {
			return _em.find(OrganizeInfDetail.class, organizeInfDetail);
		} else {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OrganizeInfDetail> getOrganizeInfDetailList(
			PCPSWebInput webInput, Integer page) {
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		String sql = "";
		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select t.* from OrganizeInfDetail t,OrganizeCustom o where t.organizeid=o.organizeid and o.issuecompanyno ='"
					+ issueCompanyNo + "'";
		} else {
			sql = "select * from OrganizeInfDetail t";
		}
		Query query = _em.createNativeQuery(sql, OrganizeInfDetail.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	// 单位归属发卡商
	@Override
	public BigInteger getOrganizeInfDetailTotal(PCPSWebInput webInput) {
		String sql = "";
		// 平台机构管理员归属机构号为空
		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
			sql = "select nvl(count(*),0) count from OrganizeInfDetail t,OrganizeCustom o where t.organizeid=o.organizeid and o.issuecompanyno ='"
					+ issueCompanyNo + "'";
		} else {
			sql = "select nvl(count(*),0) count from OrganizeInfDetail t";
		}
		Query query = _em.createNativeQuery(sql);
		return new BigInteger(query.getSingleResult().toString());
	}

	@Override
	public void insertOrganizeInfDetail(OrganizeInfDetail organizeInfDetail)
			throws PCPSApplicationException {
		GeneralDao.insert(_em, organizeInfDetail);
	}

	@Override
	public void updateOrganizeInfDetail(OrganizeInfDetail organizeInfDetail)
			throws PCPSApplicationException {
		GeneralDao.update(_em, organizeInfDetail);
	}

	@Override
	public BigInteger getMaxSeqNoByOrganizeId(BigInteger organizeId) {
		Query query = _em
				.createNativeQuery("select nvl(max(t.SeqNo),0)+1 SeqNo from OrganizeInfDetail t where  t.organizeId="
						+ organizeId);
		return new BigInteger(String.valueOf(((BigDecimal) query
				.getSingleResult())));
	}
}